package com.chezai.client.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.chezai.client.widget.NormalAlertDialog;
import com.chezai.client.widget.ProgressBarDialog;
import com.chezai.client.widget.QRDialog;

/**
 * Created by likunyang on 2017/9/22.
 */

public class DialogUtils {
    private static DialogUtils instance;
    private static ProgressBarDialog progressBarDialog;

    private DialogUtils() {
    }

    public static DialogUtils getInstance() {
        if (instance == null) {
            synchronized (DialogUtils.class) {
                if (instance == null) {
                    instance = new DialogUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 底部一个button的弹窗
     *
     * @param context
     * @param title
     * @param content
     * @param buttonText
     * @param listener
     * @return
     */
    public static NormalAlertDialog showSingleButtonAlertDialog(Context context
            , String title, String content, String buttonText
            , int dialogStyle, int tag
            , NormalAlertDialog.OnNormalAlertDialogListener listener) {
        NormalAlertDialog dialog = new NormalAlertDialog.Builder(context)
                .setTitle(title)
                .setContent(content)
                .setDialogStyle(dialogStyle)
                .setMiddleButtonText(buttonText)
                .setOnNormalAlertClickListener(listener)
                .setSingleBtnMode(true)
                .builder();
        dialog.setTag(tag);
        dialog.show();
        return dialog;
    }

    /**
     * 底部两个button的弹窗
     *
     * @param context
     * @param title
     * @param content
     * @param rightBtntext
     * @param leftBtnText
     * @param listener
     * @return
     */
    public static NormalAlertDialog showDoubleButtonAlertDialog(Context context
            , String title, String content
            , String leftBtnText, String rightBtntext
            , int tag
            , NormalAlertDialog.OnNormalAlertDialogListener listener) {
        NormalAlertDialog dialog = new NormalAlertDialog.Builder(context)
                .setTitle(title)
                .setContent(content)
                .setCancelable(false)
                .setRightButtonText(rightBtntext)
                .setLeftButtonText(leftBtnText)
                .setOnNormalAlertClickListener(listener)
                .setSingleBtnMode(false)
                .builder();
        dialog.setTag(tag);
        dialog.show();
        return dialog;
    }

    public static NormalAlertDialog showDoubleButtonAlertDialog(Context context
            , String title
            , String content
            , String leftBtnText
            , String rightBtnText
            , boolean rightBtnEnable
            , int tag
            , NormalAlertDialog.OnNormalAlertDialogListener listener) {
        NormalAlertDialog dialog = new NormalAlertDialog.Builder(context)
                .setTitle(title)
                .setContent(content)
                .setCancelable(false)
                .setRightButtonText(rightBtnText)
                .setRightButtonEnable(rightBtnEnable)
                .setLeftButtonText(leftBtnText)
                .setOnNormalAlertClickListener(listener)
                .setSingleBtnMode(false)
                .builder();
        dialog.setTag(tag);
        dialog.show();
        return dialog;
    }

    public static QRDialog showQRDialog(Context ctx, String qrUrl, QRDialog.OnCountDownTimerFinishListener listener) {
        QRDialog qrDialog = null;
        String qrPath = DeviceUtils.createQRImage(qrUrl);
        if (!TextUtils.isEmpty(qrPath)) {
            qrDialog = new QRDialog(ctx);
            qrDialog.setOnCountDownTimerFinishListener(listener);
            qrDialog.show();
            qrDialog.setQrBitmap(qrPath);
        }
        return qrDialog;
    }

    //隱藏進度框
    public static void dismissDialog() {
        try {
            if (progressBarDialog != null && progressBarDialog.isShowing()) {
                boolean isFinish = false;
                if (progressBarDialog.getContext() instanceof Activity) {
                    isFinish = ((Activity) progressBarDialog.getContext()).isFinishing() && ((Activity) progressBarDialog.getContext()).isDestroyed();
                }
                if (!isFinish) {
                    progressBarDialog.dismiss();
                }
                progressBarDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static ProgressBarDialog showProgressBarDialog(Context mContext, String msg) {
        return showProgressBarDialog(mContext, msg, null);
    }

    public static ProgressBarDialog showProgressBarDialog(Context mContext, String msg, DialogInterface.OnDismissListener lst) {

        dismissDialog();
        if (mContext == null) {
            return null;
        }
        progressBarDialog = new ProgressBarDialog(mContext, msg);
        progressBarDialog.setOnDismissListener(lst);
        progressBarDialog.show();

        return progressBarDialog;
    }

    public static ProgressBarDialog showProgressBarDialog(Context mContext, int msg) {
        return showProgressBarDialog(mContext, mContext.getString(msg));
    }
}